// -*- coding: utf-8 -*- 
/**
 * Project: Java8InActionT
 *
 * @author: yanking
 * Create time: 2022-03-20 17:19
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.Optional;

import com.broClass.Person.Person;
import com.broClass.Person.Student;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.*;

public class Car {
    private String name;
    private Optional<type> t;

    public Optional<type> getType() {
        return t;
    }

    public String getName() {
        return name;
    }

    @Test
    public void shout() {
        Map<String, Student> t = new HashMap<>();
        t.put("a", null);
        t.put("b", new Student(new Person("xiaowang", 18)));
        System.out.println(t.get("a").getName());
        Optional<Student> value = Optional.ofNullable(t.get("a"));
        value.ifPresent(a -> System.out.print(a.getName() + "\t"));
    }

    /**
     * 字符串转化为数字
     *
     * @return
     */
    public Optional<Integer> string2Int(String s) {
        try {
            return Optional.of(Integer.parseInt(s));
        } catch (NumberFormatException e) {
            return Optional.empty();
        }
    }

    @Test
    public void shout00() {
        System.out.println(string2Int("123s"));
        string2Int("123").ifPresent(System.out::println);
    }

//    @Test
//    public void shout11() {
//        ExecutorService executor = Executors.newCachedThreadPool();
//        Future<Double> future = executor.submit(new Callable<Double>() {
//
//            @Override
//            public Double call() throws Exception {
//                return doSomeLongComputation();
//            }
//        });
//
//        try {
//            Double result = future.get(1, TimeUnit.SECONDS);
//        } catch (InterruptedException e) {
//            // 中断异常
//        } catch (ExecutionException e) {
//            // 运行异常
//        } catch (TimeoutException e) {
//            // 超时
//        }
//
//
//    }

}
